﻿/******************************************************************************
	Cobalt - utility libraries for building XNA games
	Copyright (C) 2008 Philip Djonov

	This program is free software; you can redistribute it and/or modify it
	under the terms of the GNU General Public License as published by the Free
	Software Foundation; either version 2 of the License, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
	for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the
	
		Free Software Foundation, Inc.
		51 Franklin Street, Fifth Floor
		Boston, MA  02110-1301, USA.
******************************************************************************/

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Cobalt.Framework.Graphics
{
	internal sealed class SpriteSheetReader : ContentTypeReader<SpriteSheet>
	{
		protected override SpriteSheet Read( ContentReader input, SpriteSheet existingInstance )
		{
			var ret = new SpriteSheet();

			int sheetCount = input.ReadInt32();
			ret.sheetImages = new Texture2D[sheetCount];
			for( int i = 0; i < ret.sheetImages.Length; i++ )
				ret.sheetImages[i] = input.ReadRawObject<Texture2D>();

			int spriteCount = input.ReadInt32();
			ret.sprites = new SpriteSheet.SpriteDesc[spriteCount];
			for( int i = 0; i < ret.sprites.Length; i++ )
			{
				ret.sprites[i].Name = input.ReadString();
				ret.sprites[i].SheetIndex = input.ReadInt32();
				ret.sprites[i].SourceRectangle = input.ReadRawObject<Rectangle>();
			}

			return ret;
		}
	}
}